import service from '@/service/service'

// import { validatePower } from '@/service/valueValidate'

import i18n from '@/i18n'
import Vue from 'vue'

let vueI18n = new Vue({
  i18n
})

const controlChannel = {
  state: {
    checkRules: {
      kind: [
        // 请选择控制信道类型
        { required: true, message: vueI18n.$t('message.validate.resources.controlChannel.kind'), trigger: 'blur' }
      ],
      power: [
        // 请输入功率
        { required: true, message: vueI18n.$t('message.validate.resources.controlChannel.power[0]'), trigger: 'blur' }
        // { validator: validatePower, message: vueI18n.$t('message.validate.resources.controlChannel.power[1]'), trigger: 'blur' }
      ],
      up_freq_point: [
        // 请输入发送频点
        { required: true, message: vueI18n.$t('message.validate.resources.controlChannel.up_freq_point'), trigger: 'blur' }
      ],
      down_freq_point: [
        // 请输入接收频点
        { required: true, message: vueI18n.$t('message.validate.resources.controlChannel.down_freq_point'), trigger: 'blur' }
      ]
    },
    controlChannelType: [],
    controlChannelTable: []
  },
  mutations: {
    setControlChannelTable (state, data) {
      state.controlChannelTable = data
    },
    clearControlChannelTable (state) {
      state.controlChannelTable = []
    },
    setControlChannelType (state, data) {
      state.controlChannelType = data
    }
  },
  actions: {
    async getControlChannelTableData (context, payload) {
      let response = await context.dispatch(`axios`, {
        method: 'get',
        url: `/api/abs/resources/control_channels/`,
        params: {
          freq_segment: payload
        }
      })
      context.commit(`setControlChannelTable`, response.data)
    },
    async getControlChannelType (context, payload) {
      let response = await context.dispatch(`axios`, {
        method: 'get',
        url: `/api/abs/resources/control_channel_band_width/`
      })
      context.commit(`setControlChannelType`, response.data)
    }
  },
  getters: {
    getControlChannelTableData: (state) => {
      return [...state.controlChannelTable.map(item => {
        return {
          ...item,
          down_freq_begin: item.down_freq_begin / 1000,
          down_freq_end: item.down_freq_end / 1000,
          down_freq_point: item.down_freq_point / 1000,
          down_freq_point_format: service.miliFormat(item.down_freq_point / 1000),
          band_width: item.band_width / 1000,
          band_width_format: service.miliFormat(item.band_width / 1000)
        }
      })]
    },
    getControlChannelType: (state) => {
      return state.controlChannelType.map(item => {
        return {
          ...item,
          band_width: item.band_width / 1000
        }
      })
    }
  }
}
export default controlChannel
